﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;

namespace SMS.Controls
{
    public partial class SMSAutoComplete : UserControl
    {
        public SMSAutoComplete()
        {
            InitializeComponent();
            autoCompleteBox1.SelectionChanged += new SelectionChangedEventHandler(autoCompleteBox1_SelectionChanged);
        }

        public event Action<object, object> SelectionChanged;
        public event Action<string> SearchTextChanged;

        public event Action<string> TextChanged;

        public void SetFilter(AutoCompleteFilterPredicate<object> TextFilter)
        {
            autoCompleteBox1.FilterMode = AutoCompleteFilterMode.Custom;
            autoCompleteBox1.ItemFilter = TextFilter;
        }


        public void SetDataTemplate(DataTemplate template)
        {
            autoCompleteBox1.ItemTemplate = template;
        }

        public void SetValuePath(string path)
        {
            autoCompleteBox1.ValueMemberPath = path;
        }

        void autoCompleteBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
                SelectionChanged(this, autoCompleteBox1.SelectedItem);
        }

        public void SetSelectedItem(BaseEntity obj)
        {
            autoCompleteBox1.SelectedItem = obj;
        }

        public IEnumerable<BaseEntity> ItemSource
        {
            get { return (IEnumerable<BaseEntity>)GetValue(ItemSourceProperty); }
            set
            {
                SetValue(ItemSourceProperty, value);
                if (autoCompleteBox1.ItemsSource != value)
                {
                    autoCompleteBox1.ItemsSource = value;
                    autoCompleteBox1.PopulateComplete();
                    autoCompleteBox1.IsDropDownOpen = true;
                }
            }
        }



        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ItemSourceProperty =
            DependencyProperty.Register("ItemSource", typeof(IEnumerable<BaseEntity>), typeof(SMSAutoComplete), new PropertyMetadata(null, ItemSourceChanged));

        public static void ItemSourceChanged(object sender, DependencyPropertyChangedEventArgs e)
        {

        }

        private void autoCompleteBox1_TextChanged(object sender, RoutedEventArgs e)
        {
            if (TextChanged != null)
                TextChanged(autoCompleteBox1.Text);
        }

        private void autoCompleteBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (autoCompleteBox1.Text.Length >= autoCompleteBox1.MinimumPrefixLength)
            {
                if (SearchTextChanged != null)
                    SearchTextChanged.Invoke(autoCompleteBox1.Text);
            }
            else if (autoCompleteBox1.Text.Length < autoCompleteBox1.MinimumPrefixLength)
            {
                autoCompleteBox1.IsDropDownOpen = false;
                VisualStateManager.GoToState(this, "Normal", true);
            }
        }

    }
}
